package cn.wangzg.wanlistview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayAdapter<String> adapter;
    private ArrayList<String> datas;
    private int curPage=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        curPage=0;
        datas=new ArrayList<>();
        getData(0);
        adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,datas);
        final WanListView listView=findViewById(R.id.list);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setWanListViewListener(new WanListView.IWanListViewListener() {
            @Override
            public void onRefresh() {
                curPage=0;
                datas=new ArrayList<>();
                getData(0);
                adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,datas);
                listView.setAdapter(adapter);
                listView.stopRefresh();
            }

            @Override
            public void onLoadMore() {
                getData(curPage);
                adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,datas);
                curPage++;
                adapter.notifyDataSetChanged();
                listView.stopLoadMore();
            }
        });
        listView.setAdapter(adapter);
    }

    private void getData(int page) {
        for(int i=0;i<10;i++){
            datas.add("Test "+(page*10+i));
        }
        curPage++;
    }


}
